how to assign javascript variable value to the google analytics script? [migrated]

Posted by Vinoth Prakash on Pro Webmasters See other posts from Pro Webmasters or by Vinoth Prakash
Published on 2012-10-01T06:51:33Z Indexed on 2012/10/01 9:50 UTC
Read the original article Hit count: 358

I have assigned two values in the two hidden variables at server Side and accessed those values at client side using script. I have written the google analytics code. I have set two custom variable. I need to pass two values which is stored in the javascript variables to the "value" of custom variable. I have assigned the varibales but values not displaying. please telll what error i made in the script.

My aspx code
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>


</head>
<body>
    <form id="form1" runat="server">
    <div>
        <br />
        Total Pirce&nbsp; &nbsp;:
        <asp:Label ID="Label1" runat="server" Text="10"></asp:Label><br />
        &nbsp;Ship Price&nbsp; &nbsp; :
        <asp:Label ID="Label2" runat="server" Text="5"></asp:Label>
        <br />
        ------------------<br />
        Grand Total :
        <asp:Label ID="Label3" runat="server" Text="15"></asp:Label><br />
        ------------------</div>

        <asp:HiddenField ID="HiddenField1" runat="server" />
        <asp:HiddenField ID="HiddenField2" runat="server" />
    </form>


<script type="text/javascript">

  var serverhid1 = document.getElementById('HiddenField1').value; 
   var serverhid2 = document.getElementById('HiddenField2').value; 
    alert(serverhid1)
 alert(serverhid2)

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-35156990-1']);
  //Set Custom Variable
  _gaq.push(['_setCustomVar', 1, 'TotalPirce',  serverhid1  , 3]);
  _gaq.push(['_setCustomVar', 2, 'Shipping','yes', 3]);
    _gaq.push(['_setCustomVar', 3, 'GrandTotal',check(), 3]);

  _gaq.push(['_setDomainName', 'none']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>
</body>
</html>

cs Code

protected void Page_Load(object sender, EventArgs e)
        {
            HiddenField1.Value = Label1.Text;
            HiddenField2.Value = Label2.Text;
        }

© Pro Webmasters or respective owner

Related posts about google-analytics

Related posts about JavaScript